package com.shanpin.shebao.admin.employee;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shanpin.core.constant.OrderConst;
import com.shanpin.core.http.JsonView;
import com.shanpin.core.http.MJsonView;
import com.shanpin.core.status.BaseStatus;
import com.shanpin.core.utils.DateUtil;
import com.shanpin.shebao.admin.controller.ResourceController;
import com.shanpin.shebao.admin.excel.ExcelService;
import com.shanpin.shebao.admin.module.city.CityCacheService;
import com.shanpin.shebao.basedb.model.CityConfig;
import com.shanpin.shebao.common.URI;
import com.shanpin.shebao.form.HandDecListForm;
import com.shanpin.shebao.model.ServiceResult;
import com.shanpin.shebao.utility.PageModel;

@Controller
public class HandDecController extends ResourceController {
	

	@Autowired
	private ExcelService excelService;
	@Autowired
	private HandDecService handDecService;
	@Autowired
	private CityCacheService cityCacheService;
	
	@RequestMapping(URI.HAND_DEC_LIST)
	public ModelAndView listHandDec(HandDecListForm form) {
		if (StringUtils.isBlank(form.getOpt_month())) {
			if (Integer.parseInt(DateUtil.currentStr("d")) > 15) {
				form.setOpt_month(DateUtil.yyyyMM(1));
			} else {
				form.setOpt_month(DateUtil.yyyyMM());
			}
		}
		PageModel pageModel = handDecService.listDec(form, PAGE_SIZE + 40);
		ModelAndView mav = new ModelAndView("order/hand_dec");
		mav.addObject("pageModel", pageModel);
		mav.addObject("form", form);
		queryExistedCities(mav, form.getOpt_month());
		mav.addObject("list_uri", URI.SBA + URI.HAND_DEC_LIST);
		mav.addObject("hd_export_uri", URI.SBA + URI.HAND_DEC_EXPORT);
		mav.addObject("hd_apply_uri", URI.SBA + URI.HAND_DEC_MODIFY_STATUS_APPLY);
		mav.addObject("hd_ok_uri", URI.SBA + URI.HAND_DEC_MODIFY_STATUS_OK);
		mav.addObject("hd_del_uri", URI.SBA + URI.HAND_DEC_MODIFY_DEL);
		mav.addObject("view_logs_uri", URI.SBA + URI.KEFU_ORDER_VIEW_ORDER_LOGS);
		return mav;
	}
	
	
	protected void queryExistedCities(ModelAndView mav, String opt_month) {
		List<Integer> existedCities = handDecService.loadDecCitys(opt_month);

		List<CityConfig> existedCityList = new ArrayList<>();
		TreeSet<CityConfig> pCitySet = cityCacheService.cityCode2Exist(existedCities, existedCityList);
		mav.addObject("existedCityList", existedCityList);
		mav.addObject("pCitySet", pCitySet);
	}

	/**
	 * 添加手动申请减员的记录
	 * @param order_nos
	 * @param urgent 是否紧急
	 * @return 添加成功的数量 或 异常信息
	 */
	@RequestMapping(URI.HAND_DEC_ADD)
	public ModelAndView addDec(String order_nos, Integer urgent, HttpServletRequest request) {
		if (StringUtils.isBlank(order_nos)) {
			return MJsonView.resp(BaseStatus.PARAM_INVALID, "需要在网页左侧选择数据");
		}
		if (null == urgent) {
			return MJsonView.resp(BaseStatus.PARAM_INVALID, "应急情况需要有效值");
		}
		String opt_user = getLoginUser(request);
		ServiceResult<Integer> ret = handDecService.addDec(order_nos, urgent, opt_user);
		if (null != ret.result && ret.result > 0) {
			return MJsonView.ok(ret.result);
		}
		return MJsonView.fail(ret.msg);
	}
	
	@RequestMapping(URI.HAND_DEC_MODIFY_STATUS_APPLY)
	public ModelAndView updateStatusApply(String order_nos, HttpServletRequest request) {
		return updateStatus(order_nos, OrderConst.APPLYING, request);
	}
	
	@RequestMapping(URI.HAND_DEC_MODIFY_STATUS_OK)
	public ModelAndView updateStatusOk(String order_nos, HttpServletRequest request) {
		return updateStatus(order_nos, OrderConst.OK, request);
	}
	
	private ModelAndView updateStatus(String order_nos, OrderConst status, HttpServletRequest request) {
		if (StringUtils.isBlank(order_nos)) {
			return MJsonView.resp(BaseStatus.PARAM_INVALID, "需要在网页左侧选择数据");
		}
		String opt_user = getLoginUser(request);
		ServiceResult<Boolean> ret = handDecService.updateStatus(order_nos, status, opt_user);
		if (null != ret.result && ret.result) {
			return MJsonView.ok(ret.msg);
		}
		return MJsonView.fail(ret.msg);
	}
	
	/**
	 * 删除手动减员
	 * @param order_nos
	 * @param request
	 * @return 
	 * 返回删除操作的信息。
	 */
	@RequestMapping(URI.HAND_DEC_MODIFY_DEL)
	public ModelAndView del(String order_nos, HttpServletRequest request) {
		if (StringUtils.isBlank(order_nos)) {
			return MJsonView.resp(BaseStatus.PARAM_INVALID, "需要在网页左侧选择数据");
		}
		String opt_user = getLoginUser(request);
		ServiceResult<Integer> ret = handDecService.del(order_nos, opt_user);
		if (StringUtils.isNotBlank(ret.msg)) {
			return MJsonView.fail(ret.msg);
		}
		return MJsonView.ok("成功删除 " + ret.result + " 条减员记录");
	}
	
	@RequestMapping(value=URI.HAND_DEC_EXPORT)
	public void exportdec(HandDecListForm form, HttpServletResponse response) throws IOException {
		if (StringUtils.isBlank(form.getOpt_month())) {
			JsonView.fail("【所属月份】需要选择 月份").write(response);
			return;
		}
		String[] headers = { "城市", "参缴人", "身份证号", "参保人联系电话",  
				"社保OR公积金", "减员月份", "所属月份", "加急", "减员状态", 
				"最新操作人", "最新更新时间"};
		String[] fields = { "city_name", "user_name", "id_card", "phone", 
				"pay_for", "via_month", "opt_month", "urgent", "status",  
				"opt_user", "update_time"};
		List<HandDecListResp> orderList = handDecService.exportDec(form);
		if(orderList != null && orderList.size() > 0){
			String fileName = "DEC_BY_HAND_"+DateUtil.currentStr("M-dd_HH:mm")+".xls";
			excelService.exportExcel(fileName, "手动申请减员", headers,fields,orderList,response);
		}
	}
	
		
}
